<?php
/**
 * 角色管理
 */

namespace app\admin\controller\admin;


use app\common\controller\Backstage;
use think\facade\View;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("角色管理")
 */
class AdminRole extends Backstage
{

    //中间件不验证的方法
    protected $middleware = [
        'app\\middleware\\Admin' => [
            'except' => ['info','listApi']
        ]
    ];

    /**
     * @Apidoc\Title("角色列表")
     * @Apidoc\Url("/admin/admin.AdminRole/listApi")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="array", desc="返回说明")
     */
    public function listApi()
    {
        $post = $this->request->param();
        $page_size = empty($post['page_size']) ? 10 : $post['page_size'];
        $where = [];
        $where[] = ['delete_time', '=', 0];
        //关键字
        if (!empty($post['keyword'])) {
            $where[] = ['role_name|remark', 'like', "%{$post['keyword']}%"];
        }

        //排序
        $order = ['id' => 'DESC'];
        if (!empty($post['order']) && !empty($post['order_name'])) {
            $order_name = $post['order_name'];
            switch ($post['order']) {
                case 1:
                    $order = [$order_name => 'DESC'];
                    break;
                case 2:
                    $order = [$order_name => 'ASC'];
                    break;
                default:
                    $order = ['id' => 'DESC'];
            }
        }

        //数据
        $list = (new \app\common\model\AdminRole())
            ->where($where)
            ->order($order)
            ->withoutField('role_id')
            ->paginate($page_size)
            ->toArray();//每页显示数
        $list_data = $list['data'];

        foreach ($list_data as $key => $val) {

        }
        return $this->ajax(1, '成功', [
            'list' => $list_data,
            'total' => $list['total'],
            'page_size' => $page_size,
        ]);
    }


    /**
     * @Apidoc\Title("添加修改")
     * @Apidoc\Url("/admin/admin.AdminRole/edit")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id", type="int", desc="列表id")
     * @Apidoc\Param("role_name", type="string",require=true, desc="角色名字")
     * @Apidoc\Param("role_id", type="string",require=true, desc="菜单权限id")
     * @Apidoc\Returned("data", type="array", desc="返回说明")
     */
    public function edit()
    {
        $post = $this->request->param();
        try {
            $this->Validate($post, [
                'role_name|角色名字' => 'require',
                'role_id|菜单权限' => 'require|array'
            ]);
            //修改
            if (isset($post['id'])) {
                (new \app\common\model\AdminRole())
                    ->update($post);
            } else {//新增
                (new \app\common\model\AdminRole())
                    ->save($post);
            }
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getError());
        }
        return $this->ajax(1, '成功');
    }

    /**
     * @Apidoc\Title("获取详情")
     * @Apidoc\Url("/admin/AdminRole/info")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id", type="int",require=true, desc="列表id" )
     * @Apidoc\Returned("data", type="array", desc="返回说明")
     */
    public function info()
    {
        $post = $this->request->param();
        try {
            $this->Validate($post, [
                'id|系统错误' => 'require'
            ]);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getError());
        }
        $info = (new \app\common\model\AdminRole())
            ->where(['id' => $post['id']])
            ->withoutField(['create_time', 'update_time','delete_time'])
            ->find()
            ->toArray();
        //菜单权限
//        $route = (new \app\common\model\Route())
//            ->order(['weigh' => 'DESC'])
//            ->select()
//            ->toArray();
//        $list = $this->make_tree($route, $info['role_id']);
        return $this->ajax(1, '成功', [
            'info' => $info,
 //           'list' => $list,
        ]);
    }


    /**
     * @Apidoc\Title("删除")
     * @Apidoc\Url("/admin/admin.AdminRole/del")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id", type="int",require=true, desc="列表id" )
     * @Apidoc\Returned("data", type="array", desc="返回说明")
     */
    public function del()
    {
        $post = $this->request->param();
        try {
            $this->Validate($post, [
                'id|id' => 'require'
            ]);
            //删除
            (new \app\common\model\AdminRole())
                ->where(['id' => $post['id']])
                ->update([
                    'delete_time' => time()
                ]);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getError());
        }
        return $this->ajax(1, '操作成功');
    }


}
